import React, { Component } from 'react'
import './index.css'

export default class index extends Component {
  state = {
    mouse: false // 标识鼠标移入,移出
  }
  // s鼠标移入移出的回调
  handlerMouse = (flag) => {
    return () => {
      console.log(flag)
      this.setState({
        mouse: flag
      })
    }
  }
  // 勾选和取消勾选的回调
  handleClick = (id) => {
    return (event) => {
      this.props.updateTodo(id, event.target.checked)
      console.log(id, event.target.checked)
    }
  }
  // 删除一个todo 对象
  handleDelete = (id) => {
    // 获取原来的todos
    if (window.confirm('确定删除吗'))
    this.props.deleteTodo(id)
    console.log('通知app 删除',id)
  }
  render() {
    const {id, name, done} = this.props
    const {mouse} = this.state
    return (
      <li style={{backgroundColor: mouse ? '#ddd' : 'white'}} onMouseMove={this.handlerMouse(true)} onMouseLeave={this.handlerMouse(false)}>
        <label>
          <input type="checkbox" checked={done} onChange={this.handleClick(id)}/>
          <span>{name}</span>
        </label>
        <button onClick={() => this.handleDelete(id)} className="btn btn-danger" style={{display: mouse ? 'block' : 'none'}}>删除</button>
      </li>
    )
  }
}
